<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class muser extends CI_Model {

    var $theme_data = array(
        'layout_main' => 'main/user/layout',
    );
    public $mytable = "user";
    public $mytable_privilege = "privileges";
    public $primaryKey = "USE_ID";
    public $resCode = "USER";
    public $user_id;
    public $user_name;
    public $user_account;
    public $user_avatar;
    public $fb_id;

    function __construct() {
        parent::__construct();
        $this->setInfo();
    }

    function field() {
        $fieldData = array();
        $fieldData['DEP_ID'] = 0;
        $fieldData['USE_ACCOUNT'] = '';
        $fieldData['USE_NAME'] = '';
        $fieldData['USE_PASSWORD'] = '';
        $fieldData['USE_EMAIL'] = '';
        $fieldData['USE_GENDER'] = 1;
        $fieldData['USE_AVATAR'] = '';
        $fieldData['USE_PASSTIMEUPDATE'] = 0;
        return $fieldData;
    }
	
    function field_privilege() {
        $fieldData = array();
        $fieldData['USE_ID'] = 0;
        $fieldData['PRI_LEVEL'] = 0;
        $fieldData['RES_ID'] = 0;
        return $fieldData;
    }
	
    function registry($data) {
        /*
          $data["USE_LOGIN"] = $data["USE_EMAIL"];
          $data["USE_STATUS"] = 1;
          $data["TIME_CREATE"] = time();
          $data["TIME_UPDATE"] = time();
          $salt = $this->random_salt();
          $data["USE_PASSWORD"] = $this->hash_pass($data["USE_PASSWORD"], $salt) . ":" . $salt;
         */
        $this->db->insert($this->mytable, $data);
        return $this->db->insert_id();
    }

    /**
     * Hash password
     * @return String 
     */
    public function hash_pass($pass, $salt = '') {
        return md5(base64_encode($pass));
    }

    /**
     * Random Salt
     * @return String(10)
     */
    public function random_salt() {
        $d = base64_encode(md5(time() . rand()));
        return substr($d, 0, 10);
    }

    public function random_key($num_char = 20) {
        $act = base64_encode(md5(time() . rand()));
        return substr($act, 0, $num_char);
    }

    function get($val = '', $param = 'USE_ID') {
        $this->db->from($this->mytable);
        $this->db->where($param, $val);
        return $this->db->get()->first_row();
    }

    function insert($arr_insert) {
        $arr_insert = $this->msystem->insert_security($arr_insert, $this->field());
        $this->db->insert($this->mytable, $arr_insert);
        return $this->db->insert_id();
    }

    function update($val = '', $param = null, $arr_update) {
        if ($param == null)
            $param = $this->primaryKey;

        if ($val != '')
            $this->db->where($param, $val);
        $arr_update = $this->msystem->update_security($arr_update, $this->field());
        $this->db->update($this->mytable, $arr_update);
    }

    function setLogin($user) {
        if (!$user) {
            return false;
        }

        $data = array(
            'user_id' => $user->USE_ID,
            'user_account' => $user->USE_ACCOUNT,
            'user_name' => $user->USE_ACCOUNT,
            'user_email' => $user->USE_EMAIL,
            'user_avatar' => $user->USE_AVATAR,
            'isLogin' => 1,
        );
        $this->session->set_userdata($data);
        return true;
    }

    public function checkLogin() {
        return $this->session->userdata('isLogin');
    }

    function update_password($user_id = 0, $user_password = '') {
        $salt = $this->random_salt();
        $new_passwd = $this->hash_pass($user_password, $salt);
        $arr_update = array();
        $arr_update['USE_PASSWORD'] = $new_passwd . ":" . $salt;
        $arr_update['TIME_UPDATE'] = time();
        $this->db->where('USE_ID', $user_id);
        $this->db->update($this->mytable, $arr_update);
    }

    function set_activation($user_id = 0) {
        $arr_update = array();
        $arr_update['USE_STATUS'] = 1;
        $arr_update['TIME_UPDATE'] = time();
        $this->db->where('USE_ID', $user_id);
        $this->db->update($this->mytable, $arr_update);
    }

    function getAll($param = '', $val = '', $start = 0, $limit = 10) {
        $this->db->from($this->mytable);
        if ($param != '' && $val != '')
            $this->db->where($param, $val);
        $this->db->limit($limit, $start);
        return $this->db->get()->result();
    }

    function gets($val = '', $param = null) {
        if ($param == null)
            $param = 'USE_ID';

        $this->db->from($this->mytable);
        if ($val != '')
            $this->db->where($param, $val);
        $this->db->order_by("USE_ID DESC");
        return $this->db->get()->result();
    }

    function logout() {
        $this->session->sess_destroy();
        return 1;
    }

    function info($user_id) {
        
    }

    function setInfo() {
        if ($this->checkLogin()) {
            $this->user_id = $this->session->userdata('user_id');
            $this->user_account = $this->session->userdata('user_account');
            $this->user_name = $this->session->userdata('user_name');
            $this->user_avatar = $this->session->userdata('user_avatar');
        }
		
    }

    function getsUserGroup($condition = array()) {
        $this->db->from("user_group");
        $this->db->where($condition);
        return $this->db->get()->result();
    }
	
    function updatePermission($val = '', $param = null, $arr_update) {
	
		if ($param == null)
            $param = $this->primaryKey;
        if (is_array($val) && $val != array())
            $this->db->where($val);
        else if ($val != '')
            $this->db->where($param, $val);
		$arr_update = $this->msystem->update_security($arr_update, $this->field_privilege());
        $this->db->update($this->mytable_privilege, $arr_update);
    }
	
	function insertPermission($arr_insert) {
        $arr_insert = $this->msystem->insert_security($arr_insert, $this->field_privilege());
        $this->db->insert($this->mytable_privilege, $arr_insert);
        return $this->db->insert_id();
    }
	
    function deletePermission($val = '', $param = null) {
        if ($val != '') {
            if ($param == null)
                $param = $this->primaryKey;

            $this->db->where($param, $val);
            $this->db->delete("project_privilege");
        }
    }
	
}

?>